﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HT.Data.BussinessLogic;
using HT.Data.Objects;
using System.Data;
namespace TestHT.Operations
{
    public partial class Expense : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                txtStartDate.Text = DateTime.Today.AddDays(-7).ToString("MM/dd/yyyy");
                txtEndDate.Text = DateTime.Today.ToString("MM/dd/yyyy");
                LoadExpenses();
            }
        }

        protected void btnSumbit_click(object sender, EventArgs e)
        {
            LoadExpenses();
        }

        private void LoadExpenses()
        {
            DataSet ds = HotelManger.GetInstance().GetExpense(txtStartDate.Text, txtEndDate.Text);
            if (ds.Tables.Count > 0)
            {
                if (ds.Tables[0].Rows.Count > 0)
                {
                    gvExpense.DataSource = ds;
                    gvExpense.DataBind();
                }
                else
                {
                    DataTable dt = new DataTable();
                    dt.Columns.Add("ExpenseId");
                    dt.Columns.Add("ExpenseDate");
                    dt.Columns.Add("ExpenseDescription");
                    dt.Columns.Add("Amount");
                    dt.Columns.Add("Remarks");
                    dt.Columns.Add("Edit");
                    dt.Columns.Add("Delete");
                    DataRow dr = dt.NewRow();
                    dt.Rows.Add(dr);
                    gvExpense.DataSource = dt;
                    gvExpense.DataBind();
                    gvExpense.Rows[0].Visible = false;

                }
            }

        }


        protected void gvExpense_RowEditing(object sender, GridViewEditEventArgs e)
        {
            gvExpense.EditIndex = e.NewEditIndex;
            LoadExpenses();
        }

        protected void gvExpense_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {

            gvExpense.EditIndex = -1;
            LoadExpenses();
        }

        protected void gvExpense_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName.Equals("Insert"))
            {
                if (!string.IsNullOrEmpty(((TextBox)gvExpense.FooterRow.FindControl("txtDate")).Text.Trim()))
                {
                    HT.Data.Objects.Expense htExpense = new HT.Data.Objects.Expense();
                    htExpense.ExpenseDate = ((TextBox)gvExpense.FooterRow.FindControl("txtDate")).Text;
                    htExpense.ExpenseDescription = ((TextBox)gvExpense.FooterRow.FindControl("txtExpenseDescription")).Text;
                    htExpense.Remarks = ((TextBox)gvExpense.FooterRow.FindControl("txtRemarks")).Text;
                    htExpense.Amount = Convert.ToDouble(((TextBox)gvExpense.FooterRow.FindControl("txtAmount")).Text);
                    HotelManger.GetInstance().SaveExpense(htExpense);
                    LoadExpenses();
                }
            }

        }

        protected void gvExpense_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int id = Convert.ToInt32(gvExpense.DataKeys[e.RowIndex].Values[0].ToString());
            HotelManger.GetInstance().DeleteTax(id);
            LoadExpenses();
        }

        protected void gvExpense_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            HT.Data.Objects.Expense htExpense = new HT.Data.Objects.Expense();
            htExpense.ExpenseId = Convert.ToInt32(gvExpense.DataKeys[e.RowIndex].Values[0].ToString());
            htExpense.ExpenseDate = ((TextBox)gvExpense.Rows[e.RowIndex].FindControl("txtDate1")).Text;
            htExpense.ExpenseDescription = ((TextBox)gvExpense.Rows[e.RowIndex].FindControl("txExpenseDescription")).Text;
            htExpense.Remarks = ((TextBox)gvExpense.Rows[e.RowIndex].FindControl("txtRemarks")).Text;
            htExpense.Amount = Convert.ToDouble(((TextBox)gvExpense.Rows[e.RowIndex].FindControl("txtAmount")).Text);
            HotelManger.GetInstance().UpdateExpense(htExpense);
            gvExpense.EditIndex = -1;
            LoadExpenses();
        }

    }
}